{% load static %}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{% static 'admin/css/pearCommon.css' %}" />
    <link rel="stylesheet" href="{% static 'component/layui/css/layui.css' %}" />
</head>

<body>
    <form class="layui-form" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户</label>
                        <div class="layui-input-block">
                            <input type="text" name="user_name" lay-verify="required" autocomplete="off" placeholder="请输入姓名"
                                class="layui-input" value="{{ per.user.name }}" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">身份证号</label>
                        <div class="layui-input-block">
                            <input type="text" name="user_name" lay-verify="required" autocomplete="off" placeholder="请输入身份证号"
                                class="layui-input" value="{{ per.user.id_card }}" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item" style="display: none">
                        <label class="layui-form-label">用户ID</label>
                        <div class="layui-input-block">
                            <input type="text" name="user_id" lay-verify="required" autocomplete="off" placeholder="用户ID"
                                class="layui-input" value="{{ per.user.id }}">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">角色</label>
                        <div class="layui-input-block">
                            <select name="role_id" lay-verify="required" lay-filter="role">
                                <option value=""></option>
                                {% for role in roles %}
                                    {% if per.role.id == role.id %}
                                    <option value="{{role.id}}" selected="selected">{{ role.name }}</option>
                                    {% else %}
                                    <option value="{{role.id}}">{{ role.name }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">仓库</label>
                        <div class="layui-input-block">
                            {% for store in stores %}
                            <input type="checkbox" title="{{ store.name }}" value="{{ store.id }}" lay-skin="primary"
                            {% if store.id in pitch_on_per %} checked {% endif %}>
                            {% endfor %}
                            <!-- <input type="checkbox" title="11" value="22" lay-skin="primary" checked> -->
                        </div>
                    </div>
{#                    <div class="layui-form-item">#}
{#                        <label class="layui-form-label">仓库</label>#}
{#                        <div class="layui-input-block">#}
{#                            <select name="store_id" lay-verify="required" lay-filter="store">#}
{#                                <option value=""></option>#}
{#                                {% for store in stores %}#}
{#                                    {% if per.storehouse.id == store.id %}#}
{#                                    <option value="{{store.id}}" selected="selected">{{ store.name }}</option>#}
{#                                    {% else %}#}
{#                                    <option value="{{store.id}}">{{ store.name }}</option>#}
{#                                    {% endif %}#}
{#                                {% endfor %}#}
{#                            </select>#}
{#                        </div>#}
{#                    </div>#}
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script src="{% static 'component/layui/layui.js' %}"></script>
    <script src="{% static 'component/my.js' %}"></script>
    <script>
        layui.use(['form', 'jquery'], function () {
            let form = layui.form;
            let $ = layui.jquery;

            form.on('submit(user-save)', function (data) {
                // 选中的仓库
                var stores = [];
                $('input[type=checkbox]:checked').each(function () {
                    stores.push($(this).val());
                });
                //数组
                console.log(stores);
                data.field.stores = stores
                $.ajax({
                    url: "{% url 'user:permission_edit' per.id %}",
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    headers: {
                        "X-CSRFToken": getCookie("csrftoken")
                    },
                    success: function (result) {
                        if (result.status == 1) {
                            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.table.reload("user-table");
                            });
                        } else {
                            layer.msg(result.msg, { icon: 2, time: 3000 });
                        }
                    }
                })
                return false;
            });
        })
    </script>
    <script>
    </script>
</body>

</html>